package com.logindemo.springbootlogindemo.controller;



import com.logindemo.springbootlogindemo.domain.User;
import com.logindemo.springbootlogindemo.service.UserService;
import com.logindemo.springbootlogindemo.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

//  注入UserService
@RestController
@RequestMapping("/user")   // 注解@RequestMapping中的"/user"是这个控制器类的基路由
public class UserController {
    @Resource
    private UserService userService;

    // 实现登录的控制---默认账号登录
    // 表示处理post请求，路由为/user/login
    @PostMapping("/login")
    public Result<User> loginController(@RequestParam String uname, @RequestParam String password){
        User user = userService.loginService(uname, password);
        if(user!=null){
            return Result.success(user,"登录成功！");
        }else{
            return Result.error("400","账号或密码错误！");
        }
    }

    // 手机号登录---新增功能
    // 表示处理post请求，路由为/user/login/phone
    @PostMapping("/login/phone")
    public Result<User> loginByPhone(@RequestParam String phonenumber, @RequestParam String code) {
        User user = userService.loginByPhone(phonenumber, code);
        if (user != null) {
            return Result.success(user, "登录成功");
        } else {
            return Result.error("400", "手机号或验证码错误");
        }
    }

    // 邮箱登录---新增功能
    // 表示处理post请求，路由为/user/login/email
    @PostMapping("/login/email")
    public Result<User> loginByEmail(@RequestParam String email, @RequestParam String emailcode) {
        User user = userService.loginByEmail(email, emailcode);
        if (user != null) {
            return Result.success(user, "登录成功");
        } else {
            return Result.error("400", "邮箱或验证码错误");
        }
    }

    // 实现注册的控制
    // 表示处理post请求，路由为/user/register
    @PostMapping("/register")
    public Result<User> registController(@RequestBody User newUser) {
        // 直接返回 registService 的结果，不需要额外的判断
        return userService.registService(newUser);
    }
}
